<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="java,logback," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="Logback介绍Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块：logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日">
<meta name="keywords" content="java,logback">
<meta property="og:type" content="article">
<meta property="og:title" content="Logback日志配置的简单使用">
<meta property="og:url" content="http://yoursite.com/2018/01/25/pancm69/index.html">
<meta property="og:site_name" content="虚无境的博客">
<meta property="og:description" content="Logback介绍Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块：logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日">
<meta property="og:image" content="http://img.blog.csdn.net/20180125223411264?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:image" content="http://img.blog.csdn.net/20180125223435387?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">
<meta property="og:updated_time" content="2018-02-09T11:54:30.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Logback日志配置的简单使用">
<meta name="twitter:description" content="Logback介绍Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块：logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日">
<meta name="twitter:image" content="http://img.blog.csdn.net/20180125223411264?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/01/25/pancm69/"/>






  <title>Logback日志配置的简单使用 | 虚无境的博客</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?39c177d10f6e05ddfa113e02139b9c1c";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">虚无境的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/01/25/pancm69/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="虚无境">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/xuwujing.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="虚无境的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Logback日志配置的简单使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-25T22:37:00+08:00">
                2018-01-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index">
                    <span itemprop="name">java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <script src="\assets\js\APlayer.min.js"> </script><h2 id="Logback介绍"><a href="#Logback介绍" class="headerlink" title="Logback介绍"></a>Logback介绍</h2><p>Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块：logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。</p>
<h2 id="Logback的优点"><a href="#Logback的优点" class="headerlink" title="Logback的优点"></a>Logback的优点</h2><ol>
<li>执行速度快，初始化内存加载小;</li>
<li>原生实现了 SLF4J API，不需要进行转换;</li>
<li>配置简单，并可以适应多种环境;</li>
<li>可以定时的删除过期日志;</li>
<li>更为强大的过滤器，不必因更改日志级别而产生大量的日志;</li>
<li>可以从 IO 错误中进行恢复;</li>
<li>….<br>关于logback的优点还有很多，这里我就不一一列举了。详细的可以查看官方的Api文档。<br>地址:<a href="https://logback.qos.ch/documentation.html" target="_blank" rel="external">https://logback.qos.ch/documentation.html</a></li>
</ol>
<h2 id="Logback的配置说明"><a href="#Logback的配置说明" class="headerlink" title="Logback的配置说明"></a>Logback的配置说明</h2><p>关于这块的配置说明，我就简单的列举一些比较常用配置来进行说明。如果还想了解更多，请看官方的文档说明。</p>
<h3 id="Logback的层级说明"><a href="#Logback的层级说明" class="headerlink" title="Logback的层级说明"></a>Logback的层级说明</h3><p>简单的层级结构，实际有更多，仅供参考。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">configuration  </div><div class="line">    root</div><div class="line">    logger                     </div><div class="line">	property                        </div><div class="line">    appender </div><div class="line">		layout </div><div class="line">		rollingPolicy</div><div class="line">		   Pattern</div><div class="line">		   fileNamePattern</div><div class="line">		   maxFileSize</div><div class="line">		   maxHistory</div><div class="line">		   totalSizeCap</div></pre></td></tr></table></figure></p>
<h3 id="具体说明"><a href="#具体说明" class="headerlink" title="具体说明"></a>具体说明</h3><h4 id="configuration-根节点，-一般有三个属性，分别是scan、scanPeriod和debug。"><a href="#configuration-根节点，-一般有三个属性，分别是scan、scanPeriod和debug。" class="headerlink" title="configuration  根节点， 一般有三个属性，分别是scan、scanPeriod和debug。"></a>configuration  根节点， 一般有三个属性，分别是scan、scanPeriod和debug。</h4><ul>
<li>scan:是否自动加载，默认为true。</li>
<li>scanPeriod:监听修改时间间隔，默认一分钟。</li>
<li>debug: 是否查看logback运行状态，默认true。</li>
</ul>
<p>那么我们要使用这个的话，可以进行如下配置:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;configuration  scan=&quot;true&quot; scanPeriod=&quot;30 seconds&quot; debug=&quot;true&quot;&gt;</div><div class="line">...</div><div class="line">&lt;/configuration&gt;</div></pre></td></tr></table></figure>
<h3 id="root-和logger-子节点，用于指定输入的日志级别。"><a href="#root-和logger-子节点，用于指定输入的日志级别。" class="headerlink" title="root 和logger 子节点，用于指定输入的日志级别。"></a>root 和logger 子节点，用于指定输入的日志级别。</h3><ul>
<li>root ：这个是指定主日志的级别。</li>
<li><p>logger :这个是指定自定义日志的级别。</p>
<p><strong>说明</strong>:root 指定的日志级别就是用类进行输出的日志,例如:</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">private static Logger LOG = LoggerFactory.getLogger(logbackTest.class);</div></pre></td></tr></table></figure>
<p>logger 指定的日志级别是自定义的级别，例如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">private static Logger LOG2 = LoggerFactory.getLogger(&quot;oneInfo&quot;);</div></pre></td></tr></table></figure>
<p>他们的用法如下:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;logger name=&quot;oneInfo&quot; level=&quot;DEBUG&quot; additivity=&quot;false&quot;&gt;</div><div class="line">	   &lt;appender-ref ref=&quot;ONE_INFO&quot; /&gt;</div><div class="line">	&lt;/logger&gt;</div><div class="line">	&lt;root level=&quot;INFO&quot;&gt;</div><div class="line">		&lt;appender-ref ref=&quot;STDOUT&quot;/&gt;</div><div class="line">		&lt;appender-ref ref=&quot;FILE&quot; /&gt;</div><div class="line">	&lt;/root&gt;</div></pre></td></tr></table></figure></p>
<p><strong>说明二</strong>: level 是日志输出的级别，additivity表示是否在控制台打印该日志。</p>
<h4 id="property-子节点，一般用来定义变量值。有两个属性name和value。类似Java中Map的key和value。"><a href="#property-子节点，一般用来定义变量值。有两个属性name和value。类似Java中Map的key和value。" class="headerlink" title="property 子节点，一般用来定义变量值。有两个属性name和value。类似Java中Map的key和value。"></a>property 子节点，一般用来定义变量值。有两个属性name和value。类似Java中Map的key和value。</h4><ul>
<li>name: 变量的名称</li>
<li>value:  变量的值</li>
</ul>
<p>用法如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;property name=&quot;LOG_HOME&quot; value=&quot;logs/pcm&quot;/&gt;</div></pre></td></tr></table></figure>
<p>说明:value指定的文件路径，会在项目同级目录下自动生成，无需手动创建。</p>
<h4 id="appender-子节点，负责写日志的组件。有两个属性，name和class。"><a href="#appender-子节点，负责写日志的组件。有两个属性，name和class。" class="headerlink" title="appender 子节点，负责写日志的组件。有两个属性，name和class。"></a>appender 子节点，负责写日志的组件。有两个属性，name和class。</h4><ul>
<li>name: 自定义名称。</li>
<li>class:对应自定义名称的全限定名，就是使用何种方式进行日志的输出。</li>
</ul>
<p>这个非常重要，也可以说是logback的核心吧。简单的用法如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;appender name=&quot;STDOUT&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;</div><div class="line">&lt;/appender&gt;</div></pre></td></tr></table></figure>
<p><strong>说明</strong>:定义一个STDOUT名称，在控制台进行输出。 </p>
<h4 id="layout和Pattern-这两个一般是一起使用的。"><a href="#layout和Pattern-这两个一般是一起使用的。" class="headerlink" title="layout和Pattern 这两个一般是一起使用的。"></a>layout和Pattern 这两个一般是一起使用的。</h4><ul>
<li>layout:格式化日志信息;</li>
<li><p>Pattern :layout 子节点，定义输出信息的格式;</p>
<p>简单的用法如下:</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;</div><div class="line">			&lt;Pattern&gt;</div><div class="line">				%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%thread] %-5level - %msg%n</div><div class="line">			&lt;/Pattern&gt;</div><div class="line">&lt;/layout&gt;</div></pre></td></tr></table></figure>
<p><strong>说明</strong>:Pattern里面格式的%d表示输出的时间格式，%thread 表示输出的线程名称，%-5level 表示字符宽度，%msg 表示输出的信息，%n表示换行。</p>
<h4 id="rollingPolicy、fileNamePattern、maxFileSize、-maxHistory和totalSizeCap-这些一般用于对日志进行滚动，也就是日志切割管理之类的。"><a href="#rollingPolicy、fileNamePattern、maxFileSize、-maxHistory和totalSizeCap-这些一般用于对日志进行滚动，也就是日志切割管理之类的。" class="headerlink" title="rollingPolicy、fileNamePattern、maxFileSize、 maxHistory和totalSizeCap 这些一般用于对日志进行滚动，也就是日志切割管理之类的。"></a>rollingPolicy、fileNamePattern、maxFileSize、 maxHistory和totalSizeCap 这些一般用于对日志进行滚动，也就是日志切割管理之类的。</h4><ul>
<li>rollingPolicy: 决定日志滚动行为，一般用于日志切割。</li>
<li>fileNamePattern:必要的节点，一般用于指定日志的文件的路径以及生成格式。</li>
<li>maxFileSize:单个日志文件最大值，达到之后就进行切割。</li>
<li>maxHistory:日志文件最多保留的天数，也就是过期时间。</li>
<li>totalSizeCap : 日志文件总体的最大值，达到之后就不再产生日志。</li>
</ul>
<p>简单的用法如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;appender name=&quot;FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;</div><div class="line">		&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy&quot;&gt;</div><div class="line">			&lt;fileNamePattern&gt;$&#123;LOG_HOME&#125;/mylog-%d&#123;yyyy-MM-dd&#125;.%i.txt&lt;/fileNamePattern&gt;</div><div class="line">			&lt;maxFileSize&gt;10MB&lt;/maxFileSize&gt;</div><div class="line">			&lt;maxHistory&gt;31&lt;/maxHistory&gt;</div><div class="line">			&lt;totalSizeCap&gt;10GB&lt;/totalSizeCap&gt;</div><div class="line">		&lt;/rollingPolicy&gt;</div><div class="line">	&lt;/appender&gt;</div></pre></td></tr></table></figure>
<p><strong>说明</strong>:这段的配置意义是，每天产生一个日志文件，如果超出了10M，日志就进行切割，并且在日志文件名称上加一，日志文件最多保持31天，日志文件总共最大为10G。</p>
<h2 id="Logback使用说明"><a href="#Logback使用说明" class="headerlink" title="Logback使用说明"></a>Logback使用说明</h2><p>在对logback日志的配置文件进行了简单的说明之后，那我们来简单的使用logback吧。<br>logback.xml 使用需要依赖三个 jar 包，分别是 slf4j-api，logback-core，logback-classic。<br>mavan配置如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">&lt;dependency&gt;</div><div class="line">   	 &lt;groupId&gt;org.slf4j&lt;/groupId&gt;</div><div class="line">   	 &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;</div><div class="line">   	 &lt;version&gt;1.7.25&lt;/version&gt;</div><div class="line">	&lt;/dependency&gt;</div><div class="line">    </div><div class="line">    </div><div class="line">    &lt;dependency&gt;</div><div class="line">    	&lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;</div><div class="line">    	&lt;artifactId&gt;logback-classic&lt;/artifactId&gt;</div><div class="line">    	&lt;version&gt;1.2.3&lt;/version&gt;</div><div class="line">	&lt;/dependency&gt;</div><div class="line">    </div><div class="line">    &lt;dependency&gt;</div><div class="line">    	&lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;</div><div class="line">    	&lt;artifactId&gt;logback-core&lt;/artifactId&gt;</div><div class="line">    	&lt;version&gt;1.2.3&lt;/version&gt;</div><div class="line">	&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<p>成功在maven添加依赖包之后，我们写个简单的demo来测试下吧。<br>定义三个log日志，一个主log，两个自定义log。<br>整体配置如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</div><div class="line">&lt;configuration  scan=&quot;true&quot; scanPeriod=&quot;30 seconds&quot; debug=&quot;true&quot;&gt;</div><div class="line">	&lt;property name=&quot;LOG_HOME&quot; value=&quot;logs/pcm&quot;/&gt;</div><div class="line">	&lt;appender name=&quot;STDOUT&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;</div><div class="line">		&lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;</div><div class="line">			&lt;Pattern&gt;</div><div class="line">				%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%thread] %-5level - %msg%n</div><div class="line">			&lt;/Pattern&gt;</div><div class="line">		&lt;/layout&gt;</div><div class="line">	&lt;/appender&gt;</div><div class="line">	&lt;appender name=&quot;FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;</div><div class="line">		&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy&quot;&gt;</div><div class="line">			&lt;fileNamePattern&gt;$&#123;LOG_HOME&#125;/mylog-%d&#123;yyyy-MM-dd&#125;.%i.txt&lt;/fileNamePattern&gt;</div><div class="line">			&lt;maxFileSize&gt;10MB&lt;/maxFileSize&gt;</div><div class="line">			&lt;maxHistory&gt;31&lt;/maxHistory&gt;</div><div class="line">			&lt;totalSizeCap&gt;10GB&lt;/totalSizeCap&gt;</div><div class="line">		&lt;/rollingPolicy&gt;</div><div class="line">		&lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;</div><div class="line">			&lt;Pattern&gt;</div><div class="line">				%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%thread] %-5level - %msg%n</div><div class="line">			&lt;/Pattern&gt;</div><div class="line">		&lt;/layout&gt;</div><div class="line">	&lt;/appender&gt;</div><div class="line">	</div><div class="line">    &lt;appender name=&quot;ONE_INFO&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;</div><div class="line">		&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy&quot;&gt;</div><div class="line">			&lt;!-- rollover daily --&gt;</div><div class="line">			&lt;fileNamePattern&gt;$&#123;LOG_HOME&#125;/oneInfo/%d&#123;yyyy-MM-dd&#125;/oneInfo.%i.txt&lt;/fileNamePattern&gt;</div><div class="line">			&lt;maxFileSize&gt;10MB&lt;/maxFileSize&gt;</div><div class="line">			&lt;maxHistory&gt;31&lt;/maxHistory&gt;</div><div class="line">			&lt;totalSizeCap&gt;10GB&lt;/totalSizeCap&gt;</div><div class="line">		&lt;/rollingPolicy&gt;</div><div class="line">		&lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;</div><div class="line">			&lt;Pattern&gt;</div><div class="line">				%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%thread] %-5level - %msg%n</div><div class="line">			&lt;/Pattern&gt;</div><div class="line">		&lt;/layout&gt;</div><div class="line">	&lt;/appender&gt;</div><div class="line">	</div><div class="line">    &lt;appender name=&quot;TWO_INFO&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;</div><div class="line">		&lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy&quot;&gt;</div><div class="line">			&lt;!-- rollover daily --&gt;</div><div class="line">			&lt;fileNamePattern&gt;$&#123;LOG_HOME&#125;/twoInfo/%d&#123;yyyy-MM-dd&#125;/twoInfo.%i.txt&lt;/fileNamePattern&gt;</div><div class="line">			&lt;maxFileSize&gt;10MB&lt;/maxFileSize&gt;</div><div class="line">			&lt;maxHistory&gt;31&lt;/maxHistory&gt;</div><div class="line">			&lt;totalSizeCap&gt;10GB&lt;/totalSizeCap&gt;</div><div class="line">		&lt;/rollingPolicy&gt;</div><div class="line">		&lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;</div><div class="line">			&lt;Pattern&gt;</div><div class="line">				%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%thread] %-5level - %msg%n</div><div class="line">			&lt;/Pattern&gt;</div><div class="line">		&lt;/layout&gt;</div><div class="line">	&lt;/appender&gt;</div><div class="line">	</div><div class="line">	&lt;logger name=&quot;oneInfo&quot; level=&quot;DEBUG&quot; additivity=&quot;false&quot;&gt;</div><div class="line">	   &lt;appender-ref ref=&quot;ONE_INFO&quot; /&gt;</div><div class="line">	&lt;/logger&gt;</div><div class="line">	&lt;logger name=&quot;twoInfo&quot; level=&quot;WARN&quot; additivity=&quot;true&quot;&gt;</div><div class="line">	   &lt;appender-ref ref=&quot;TWO_INFO&quot; /&gt;</div><div class="line">	&lt;/logger&gt;</div><div class="line">	</div><div class="line">	&lt;root level=&quot;INFO&quot;&gt;</div><div class="line">		&lt;appender-ref ref=&quot;STDOUT&quot;/&gt;</div><div class="line">		&lt;appender-ref ref=&quot;FILE&quot; /&gt;</div><div class="line">	&lt;/root&gt;</div><div class="line"></div><div class="line">&lt;/configuration&gt;</div></pre></td></tr></table></figure>
<p> 然后分别在控制台输出和文件中进行输出。<br> 该java的代码示例如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line">import org.slf4j.Logger;</div><div class="line">import org.slf4j.LoggerFactory;</div><div class="line"></div><div class="line">/**</div><div class="line"> * </div><div class="line">* Title: logbackTest</div><div class="line">* Description:</div><div class="line">* logback日志测试 </div><div class="line">* Version:1.0.0  </div><div class="line">* @author pancm</div><div class="line">* @date 2018年1月24日</div><div class="line"> */</div><div class="line">public class logbackTest &#123;</div><div class="line">	private static Logger LOG = LoggerFactory.getLogger(logbackTest.class);</div><div class="line">	private static Logger LOG2 = LoggerFactory.getLogger(&quot;oneInfo&quot;);</div><div class="line">	private static Logger LOG3 = LoggerFactory.getLogger(&quot;twoInfo&quot;);</div><div class="line">	</div><div class="line">	public static void main(String[] args) &#123;</div><div class="line">		test();</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	private static void test()&#123;</div><div class="line"></div><div class="line">		LOG.debug(&quot;主程序的debug&quot;);</div><div class="line">		LOG.info(&quot;主程序的info&quot;);</div><div class="line">		LOG.warn(&quot;主程序的warn&quot;);</div><div class="line">		LOG.error(&quot;主程序的error&quot;);</div><div class="line">		</div><div class="line">		LOG2.debug(&quot;oneInfo的debug&quot;);</div><div class="line">		LOG2.info(&quot;oneInfo的info&quot;);</div><div class="line">		LOG2.warn(&quot;oneInfo的warn&quot;);</div><div class="line">		LOG2.error(&quot;oneInfo的error&quot;);</div><div class="line">		</div><div class="line">		LOG3.debug(&quot;twoInfo的debug&quot;);</div><div class="line">		LOG3.info(&quot;twoInfo的info&quot;);</div><div class="line">		LOG3.warn(&quot;twoInfo的warn&quot;);</div><div class="line">		LOG3.error(&quot;twoInfo的error&quot;);</div><div class="line">	&#125;</div></pre></td></tr></table></figure>
<p>输出结果如下:</p>
<p><img src="http://img.blog.csdn.net/20180125223411264?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<p>日志的生成目录:<br><img src="http://img.blog.csdn.net/20180125223435387?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWF6d3N4cGNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述"></p>
<p>输出结果的说明:</p>
<ul>
<li>LOG：  因为设置打印的级别是info，所以debug级别的不会打印 。</li>
<li>LOG2: 因为自定义配置设定的是  additivity=”false”  不在控制台打印。 所以一条都不会打印，但是debug级别以上的日志可以在logs/pcm/oneInfo中查看。</li>
<li>LOG3:  因为自定义配置设定的是 additivity=”true”  可以在控制台打印。所以会打印两条warn级别的日志，日志也可以在logs/pcm/oneInfo中查看。</li>
</ul>
<h2 id="其它"><a href="#其它" class="headerlink" title="其它"></a>其它</h2><p>参考:<br><a href="https://logback.qos.ch/documentation.html" target="_blank" rel="external">https://logback.qos.ch/documentation.html</a><br><a href="https://www.cnblogs.com/warking/p/5710303.html" target="_blank" rel="external">https://www.cnblogs.com/warking/p/5710303.html</a></p>
<p>到此，本文的logback日志简单讲解结束，谢谢阅读！</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>
     
    <div>
	 +
	  
<div style="text-align:center;color: #ccc;font-size:14px;">
------ 本文结束 ------</div>
<br/>
<div style="border: 1px solid black">
<div style="margin-left:10px">
<span style="font-weight:blod">版权声明</span>
<!-- <img src="/images/xuwujing.png" > -->
<br/>
<p style="font-size: 10px;line-height: 30px"><a href="http://www.panchengming.com/" style="color:#258FC6">xuwujing's Notes</a> by <a href="http://www.panchengming.com/" style="color:#258FC6">ChengMing Pan</a> is licensed under a <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/" style="color:#258FC6">Creative Commons BY-NC-ND 4.0 International License</a>.<br/>
由<a href="http://www.panchengming.com/" style="color:#258FC6">虚无境</a>创作并维护的<a href="http://www.panchengming.com/" style="color:#258FC6">xuwujing's Notes</a>博客采用<a href="https://creativecommons.org/licenses/by-nc-nd/4.0/" style="color:#258FC6">创作共用保留署名-非商业-禁止演绎4.0国际许可证</a>。<br/>
本文首发于<a href="http://www.panchengming.com/" style="color:#258FC6">xuwujing's Notes</a> 博客（ <a href="http://www.panchengming.com/" style="color:#258FC6">http://www.panchengming.com/</a> ），版权所有，侵权必究。</p>
</div>
</div>

	
	</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/java/" rel="tag"># java</a>
          
            <a href="/tags/logback/" rel="tag"># logback</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/01/20/pancm68/" rel="next" title="大数据学习系列之九---- Hive整合Spark和HBase以及相关测试">
                <i class="fa fa-chevron-left"></i> 大数据学习系列之九---- Hive整合Spark和HBase以及相关测试
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/01/26/pancm70/" rel="prev" title="kafka和storm集群的环境安装">
                kafka和storm集群的环境安装 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/xuwujing.png"
               alt="虚无境" />
          <p class="site-author-name" itemprop="name">虚无境</p>
           
              <p class="site-description motion-element" itemprop="description">The way of the future!</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">136</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">30</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">59</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/xuwujing" target="_blank" title="github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  github
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://blog.csdn.net/qazwsxpcm?viewmode=list" target="_blank" title="csdn">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  csdn
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://home.cnblogs.com/u/xuwujing/" target="_blank" title="cnblogs">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  cnblogs
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.woainia.site/" title="woainia" target="_blank">woainia</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://cmsblogs.com/" title="chenssy" target="_blank">chenssy</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://italker.imisty.cn" title="xiaowu" target="_blank">xiaowu</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Logback介绍"><span class="nav-number">1.</span> <span class="nav-text">Logback介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Logback的优点"><span class="nav-number">2.</span> <span class="nav-text">Logback的优点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Logback的配置说明"><span class="nav-number">3.</span> <span class="nav-text">Logback的配置说明</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Logback的层级说明"><span class="nav-number">3.1.</span> <span class="nav-text">Logback的层级说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#具体说明"><span class="nav-number">3.2.</span> <span class="nav-text">具体说明</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#configuration-根节点，-一般有三个属性，分别是scan、scanPeriod和debug。"><span class="nav-number">3.2.1.</span> <span class="nav-text">configuration  根节点， 一般有三个属性，分别是scan、scanPeriod和debug。</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#root-和logger-子节点，用于指定输入的日志级别。"><span class="nav-number">3.3.</span> <span class="nav-text">root 和logger 子节点，用于指定输入的日志级别。</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#property-子节点，一般用来定义变量值。有两个属性name和value。类似Java中Map的key和value。"><span class="nav-number">3.3.1.</span> <span class="nav-text">property 子节点，一般用来定义变量值。有两个属性name和value。类似Java中Map的key和value。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#appender-子节点，负责写日志的组件。有两个属性，name和class。"><span class="nav-number">3.3.2.</span> <span class="nav-text">appender 子节点，负责写日志的组件。有两个属性，name和class。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#layout和Pattern-这两个一般是一起使用的。"><span class="nav-number">3.3.3.</span> <span class="nav-text">layout和Pattern 这两个一般是一起使用的。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rollingPolicy、fileNamePattern、maxFileSize、-maxHistory和totalSizeCap-这些一般用于对日志进行滚动，也就是日志切割管理之类的。"><span class="nav-number">3.3.4.</span> <span class="nav-text">rollingPolicy、fileNamePattern、maxFileSize、 maxHistory和totalSizeCap 这些一般用于对日志进行滚动，也就是日志切割管理之类的。</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Logback使用说明"><span class="nav-number">4.</span> <span class="nav-text">Logback使用说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#其它"><span class="nav-number">5.</span> <span class="nav-text">其它</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">虚无境</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
 | 
本站总访问量<span id="busuanzi_value_site_pv"></span>次
 | 
本站访客数<span id="busuanzi_value_site_uv"></span>人次
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=857896&auto=0&height=66"></iframe>



        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
